import random
from datetime import datetime

from flask import current_app, jsonify
from flask import json
from flask import make_response
from flask import session

from info import constants, db
from info import redis_store
from info.libs.yuntongxun.sms import CCP
from info.models import User
from info.utils.captcha.captcha import captcha
from info.utils.captcha.response_code import RET
from . import passport_blue
from flask import request
import re

@passport_blue.route('/logout',methods=['POST'])
def logout():
    session.pop("user_id",None)
    session.pop("nick_name",None)
    session.pop("mobile",None)

    return jsonify(errno=RET.OK,errmsg="退出登录")


@passport_blue.route('/login', methods=['POST'])
def login():
    # - 1.获取请求参数
    mobile = request.json.get("mobile")
    password = request.json.get("password")

    # - 2.校验参数,为空校验
    if not all([mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # - 3.根据用户手机号取出,用户对象
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取用户失败")

    # - 4.判断用户对象是否存在
    if not user:
        return jsonify(errno=RET.NODATA, errmsg="该用户不存在")

    # - 5.校验密码是否正确
    if user.password_hash != password:  #未加密登录
    # if not user.check_password(password): #加密登录
        return jsonify(errno=RET.DATAERR, errmsg="密码错误")

    # - 6.记录用户登陆信息到session
    session["user_id"] = user.id
    session["nick_name"] = user.nick_name
    session["mobile"] = user.mobile


    #记录用户最后一次登录时间
    user.last_login = datetime.now()
    return jsonify(errno=RET.OK,errmsg="登录成功")


    # - 7.返回响应
    return jsonify(errno=RET.OK, errmsg="登陆成功")


@passport_blue.route('/register', methods=['POST'])
def register():
    # - 1.获取请求参数(手机号,短信验证码,密码)
    # json_data = request.data
    # dict_data = json.loads(json_data)
    # 上面两句话合成一句话
    dict_data = request.get_json()  # 等价于request.json
    mobile = dict_data.get("mobile")
    sms_code = dict_data.get("sms_code")
    password = dict_data.get("password")

    # - 2.校验参数,为空校验
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # - 3.通过手机号作为key取出redis中的短信验证码
    try:
        redis_sms_code = redis_store.get("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="短信验证码获取失败")

    # - 4.判断短信验证码是否过期
    if not redis_sms_code:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码已过期")

    # - 5.删除redis中的短信验证码
    try:
        redis_store.delete("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.NODATA, errmsg="短信验证码删除异常")

    # - 6.判断redis中的短信验证码和传入进来的短信验证码是否相等
    if redis_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码填写错误")

    # - 7.创建用户对象,设置属性
    user = User()
    user.nick_name = mobile
    # user.password_hash = password  # 未加密
    user.password = password  # 加密
    user.mobile = mobile
    user.signature = "这个人很懒,什么都没有"

    # - 8.保存用户到数据库
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="注册用户失败")

    # - 9.返回响应
    return jsonify(errno=RET.OK, errmsg="注册成功")








#功能描述，获取图片验证码

@passport_blue.route("/sms_code",methods=['POST'])
def sms_code():



    json_data = request.data
    dict_data = json.loads(json_data)
    mobile = dict_data.get("mobile")
    image_code = dict_data.get("image_code")
    image_code_id = dict_data.get("image_code_id")

    if not all([mobile,image_code,image_code_id]):
        return jsonify(errno=RET.PARAMERR,crrmsg="参数不完整")

    if not re.match("1[356789]\\d{9}",mobile):
        return jsonify(errno=RET.DATAERR,errmsg="手机号格式不正确")

    try:
        redis_image_code = redis_store.get("image_code:%s"%image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="图片验证码获取失败")
    if not redis_image_code:
        return jsonify(errno=RET.NODATA,errmsg="图片验证码已国企")

    try:
        redis_store.delete("image_code:%s"%image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return  jsonify(errno=RET.DBERR,errmsg="删除图片验证吗失败")

    if image_code.lower() != redis_image_code.lower():
        return  jsonify(errno=RET.DATAERR,errmsg="图片验证码填写错误")

    sms_code = "%06d"%random.randint(0,999999)
    print(sms_code)
    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg="运通寻一场")

    if result == -1:
        return jsonify(errno=RET.DATAERR,errmsg="短信发送失败")
    try:
        redis_store.set("sms_code:%s"%mobile,sms_code,constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.errror(e)
        return jsonify(errno=RET.DBERR,errmsg="短信验证码保存失败")

    return jsonify(errno=RET.OK,errmsg="发送成功")


@passport_blue.route('/image_code')
def image_code():


    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")

    if not cur_id:
        return "图片验证码编号不全"
        # 生成图片验证码
    name, text, image_data = captcha.generate_captcha()

    try:
        redis_store.set("image_code:%s" % cur_id, text, constants.IMAGE_CODE_REDIS_EXPIRES)

        if pre_id:
            redis_store.delete("image_code:%s"%pre_id)

    except Exception as e:
        current_app.logger.error(e)
        return "图片验证码保存失败"
    #返回 图片验证码
    response = make_response(image_data)
    response.headers["Content-Type:"] = "image/png"
    return response